<template>
	<view>
		<view class="logoShell">
			<image class="logo" mode="widthFix" src="../../static/image/memberLogo.png"></image>
			<view class="logoText">欢迎使用云茅智能员工端</view>
			<view v-if="!isLogin">
				<button v-if="canIUseGetUserProfile" class="loginButton" @click="getUserProfile">
					<!-- <image class="reg-wx" src="../../static/image/moduleTherr.png"></image> -->
					<view class="reg-btn" bindtap="">授权微信信息</view>
				</button>
				<button v-else class="loginButton" open-type="getUserInfo" @getuserinfo="getUserInfo">
					<!-- <image class="reg-wx" src="../../static/image/moduleTherr.png"></image> -->
					<view class="reg-btn" bindtap="">授权微信信息</view>
				</button>
			</view>

			<button class="loginButton" v-if="isLogin&&!isPhone" open-type="getPhoneNumber" @getphonenumber="getPhone">
				<!-- <image class="reg-wx" src="../../static/image/moduleTherr.png"></image> -->
				<view class="reg-btn" bindtap="">授权手机号</view>
			</button>
			<view class="briefText">
				<view>本小程序仅支持内部员工使用</view>
				<view class="hintText" style="width: 327rpx;">
					需要在登陆时进行授权您的微信信息 以及手机号码,我们仅使用此资料进行 人员比对并对您的信息进行严格保密
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				isLogin: false,
				isPhone: true,
				pageType: 1, //1主页跳转  2取件码详情跳转
				memberId: "",
				pickUpCode: "",
				canIUseGetUserProfile: false, //是否授权用户信息
			}
		},

		onLoad(options) {
			if (options.type) {
				this.pageType = options.type
			}
			const openid = uni.getStorageSync("openid")
			const phone = uni.getStorageSync("phone")

			// console.log(openid, phone)
			if (openid != '' && phone != '') {
				this.registerUser(openid)
			}
			if (!openid) {
				this.isLogin = false
			} else {
				this.isLogin = true
			}
			if (!phone) {
				this.isPhone = false
			} else {
				this.isPhone = true
			}
			// if (options.memberId) {
			// 	this.memberId = options.memberId
			// }
			// if (options.pickUpCode) {
			// 	this.pickUpCode = options.pickUpCode
			// }
			if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true
			}
		},
		methods: {
			getUserProfile(e) {
				uni.getUserProfile({
					desc: "用户完善用户信息",
					success: res => {
						
						// this.userInfo = res.userInfo,
						uni.setStorage({
							key: "userInfo",
							data: res.userInfo
						})
						this.isLogin = true
						this.loginHandle()
					},
					fail: err => {
						// console.log(err)
						this.isLogin = false
					}
				})
			},

			getUserInfo(e) {
				// console.log(e)
				// return 
				// console.log('dsa')
				uni.setStorage({
					key: "userInfo",
					data: e.detail.userInfo
				})
				if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
					this.isLogin = false
				} else {
					this.loginHandle()
				}
			},

			loginHandle() {
				uni.login({
					provider: "weixin",
					success: (res) => {
						// console.log(res.code)
						this.http({
							url: "/Main/getOpenidAndPhone",
							data: {
								code: res.code
							},
							method: "POST",
							success: res1 => {
								// console.log("获取的openid：")
								// console.log(res1)
								if (res1.code == 200) {
									if (!res1.data.openid || res1.data.openid == '' || !res1.data
										.session_key || res1.data.session_key == '' || !res1.data
										.appsecret || res1.data.appsecret == '') {
										this.isLogin = false
									} else {
										uni.setStorageSync("openid", res1.data.openid)
										uni.setStorageSync("session_key", res1.data.session_key)
										uni.setStorageSync("appsecret", res1.data.appsecret)
										this.isLogin = true
									}
								}
							}
						})
					},
					fail: (err) => {
						// console.log(err)
						this.isLogin = false
					}
				})
			},
			getPhone(e) {
				// console.log("获取手机号：")
				// console.log(e)
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					this.isPhone = false
				} else {
					this.isPhone = true
					var sex;
					const userInfo = uni.getStorageSync("userInfo")
					if (userInfo.gender == 1 || userInfo.gender == 2) {
						sex = userInfo.gender
					} else {
						sex = 3
					}
					this.http({
						url: "/goods/getPhone",
						data: {
							session_key: uni.getStorageSync("session_key"),
							openid: uni.getStorageSync("openid"),
							appid: app.globalData.appid,
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv
						},
						method: "POST",
						success: res => {
							console.log("获取手机号：")
							console.log(res)
							if (res.code == 200) {
								uni.setStorage({
									key: "phone",
									data: res.data
								})
								this.registerHandle(userInfo.nickName, res.data, userInfo.avatarUrl, sex)
							}
						}
					})
				}
			},
			// 用户登录注册 userInfo.gender //性别 0：未知、1：男、2：女
			registerHandle(petName, phone, headPortrait, sex) {
				this.http({
					url: "/Main/login",
					data: {
						phone,
						open_id: uni.getStorageSync("openid"),
						headPortrait,
						// sex
					},
					method: "POST",
					success: res => {
						if (res.code == 200) {
							uni.setStorage({
								key: "memberId",
								data: res.data
							})
							// console.log(res.data)
							// loginState  2,// 1-员工 2-商户/代理商 3-员工+商户/代理商
							uni.setStorageSync('userData', res.data)
							if (res.data.loginState == 1 || res.data.loginState == 3) {
								const userInfo = uni.getStorageSync("userInfo")
								userInfo.id = res.data.user.customerId
								uni.setStorageSync('userInfo', userInfo)
								// console.log('怎么不跳？')
								uni.reLaunch({
									url: "/pages/index/index"
								})
							} else if (res.data.loginState == 2) {
								uni.reLaunch({
									url: "/pages/BillMessage/BillMessage"
								})
							} else {
								uni.showModal({
									content: '本小程序仅支持内部员工使用',
									showCancel: false
								})
							}
							// return
							// if (this.pageType == 1) {
							// 	uni.switchTab({
							// 		url: "/pages/home/home"
							// 	})
							// } else if (this.pageType == 2) {
							// 	uni.reLaunch({
							// 		url: `/pages/mineHome/takeNumDetail/takeNumDetail?pageType=1&memberId=${this.memberId}&pickUpCode=${this.pickUpCode}`
							// 	})
							// }
						}
					}
				})
			},
			// 以授权过的登陆
			registerUser(openid) {
				this.http({
					url: "/Main/secondLogin",
					data: {
						open_id: openid,
						// sex
					},
					method: "POST",
					success: res => {
						if (res.code == 200) {
							uni.setStorage({
								key: "memberId",
								data: res.data
							})
							// console.log(res.data)
							// loginState  2,// 1-员工 2-商户/代理商 3-员工+商户/代理商
							uni.setStorageSync('userData', res.data)
							if (res.data.loginState == 1 || res.data.loginState == 3) {
								const userInfo = uni.getStorageSync("userInfo")
								userInfo.id = res.data.user.customerId
								uni.setStorageSync('userInfo', userInfo)
								// console.log('怎么不跳？')
								uni.reLaunch({
									url: "/pages/index/index"
								})
							} else if (res.data.loginState == 2) {
								uni.reLaunch({
									url: "/pages/BillMessage/BillMessage"
								})
							} else {
								// console.log('我胡汉三又回来了')
								uni.clearStorage();
								
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.logoShell {
		width: 80%;
		margin: 0 auto;
		position: relative;
		top: 30%;
	}

	.logo {
		width: 280rpx;
		height: 120rpx;
		display: block;
		margin: 120rpx auto;
		margin-bottom: 90rpx;
	}
     .hintText{
		 text-align: center;
	 }
	.logoText {
		width: 100%;
		text-align: center;
		font-size: 36rpx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: #000000;
	}

	.loginButton {
		margin: 80rpx auto;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 420rpx;
		height: 104rpx;
		background: #FFFFFF;
		border: 1rpx solid #FF0B0B;
		border-radius: 40rpx;
		margin-top: 200rpx;
	}

	.reg-wx {
		width: 45rpx;
		height: 44rpx;
		margin-right: 12rpx;
	}

	.reg-btn {
		opacity: 1;
		text-align: center;
		font-size: 36rpx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: #000000;
	}

	.briefText {
		height: 120rpx;
		font-size: 20rpx;
		font-family: Source Han Sans CN;
		font-weight: 500;
		line-height: 34rpx;
		color: #000000;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 300rpx;
	}
</style>
